package com.yqb.study.modular.controller.data;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.yqb.study.annotation.log.OperateLogAnt;
import com.yqb.study.common.controller.BaseController;
import com.yqb.study.common.log.LogObjectHolder;
import com.yqb.study.common.pagination.PageFactory;
import com.yqb.study.common.pagination.PageInfoRspBT;
import com.yqb.study.constant.CodeMsgEnum;
import com.yqb.study.constant.log.LogTypeEnum;
import com.yqb.study.constant.swagger.SwaggerParamType;
import com.yqb.study.modular.persistence.model.City;
import com.yqb.study.modular.service.ICityService;
import com.yqb.study.modular.warpper.CityWarpper;
import com.yqb.study.modular.warpper.log.AuthLogWarpperMap;
import com.yqb.study.modular.warpper.log.CityLogWarpperMap;
import com.yqb.study.support.HttpKit;
import com.yqb.study.util.ResultVoUtil;
import com.yqb.study.util.ToolUtil;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;

@RestController
@Slf4j
@RequestMapping("/data")
public class CityController extends BaseController {
	
    @Autowired
    private ICityService cityService;
    
    // 查
	@ApiOperation("分页查询城市信息")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = SwaggerParamType.QUERY, name = "provinceId", value = "省份标识", required = false, dataType = "String"), 
		@ApiImplicitParam(paramType = SwaggerParamType.QUERY, name = "cityName", value = "城市名称", required = false, dataType = "String"),
		@ApiImplicitParam(paramType = SwaggerParamType.QUERY, name = "limit", value = "每页记录数", required = true, dataType = "int", defaultValue = "14"),
		@ApiImplicitParam(paramType = SwaggerParamType.QUERY, name = "offset", value = "记录偏移量", required = true, dataType = "int", defaultValue = "1"),
		@ApiImplicitParam(paramType = SwaggerParamType.QUERY, name = "sort", value = "排序字段", required = true, dataType = "String", defaultValue = "id"),
		@ApiImplicitParam(paramType = SwaggerParamType.QUERY, name = "order", value = "排序方向", required = true, dataType = "String", defaultValue = "asc")	
	})	    
	@OperateLogAnt(logTypeEnum = LogTypeEnum.QUERY_LOG, logName = "分页查询城市信息", logWapperClazz = CityLogWarpperMap.class)
	@GetMapping("/citySelect")
	public Object citySelect() {
		// Page泛型指的是每条记录查出来的类型
		// 方法defaultPage是在page对象中存放请求的limit、offset、sort、order参数
		Page<Map<String, Object>> page = new PageFactory<Map<String, Object>>().defaultPage();
		// 约定的搜索条件
		Map<String, String> searchCond = HttpKit.getRequestParameters();

		List<Map<String, Object>> cityInfoMapList = cityService.getPageCityInfoMapFromClusterDb(page, searchCond);

		log.info("查询参数是：" + searchCond.toString());
		log.info("查询结果是：" + cityInfoMapList.toString());

		// 查询结果包装
		page.setRecords((List<Map<String, Object>>) new CityWarpper(cityInfoMapList).warp());

		// 通用返回包装
		PageInfoRspBT<Map<String, Object>> pageInfoRspBT = super.packForBT(page);
		return ResultVoUtil.success(CodeMsgEnum.QUERY_SUCCESS, pageInfoRspBT);
	}

    // 增
	@ApiOperation("添加城市信息")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = SwaggerParamType.QUERY, name = "provinceId", value = "省份标识", required = true, dataType = "String", defaultValue = "3"), 
		@ApiImplicitParam(paramType = SwaggerParamType.QUERY, name = "cityName", value = "城市名称", required = false, dataType = "String", defaultValue = "苏州"),
		@ApiImplicitParam(paramType = SwaggerParamType.QUERY, name = "description", value = "描述", required = false, dataType = "String", defaultValue = "美女很多")	
	})	  	
    @OperateLogAnt(logTypeEnum = LogTypeEnum.ADD_LOG, logName = "添加城市信息", logWapperClazz = CityLogWarpperMap.class)
    @GetMapping("/cityAdd")
    public Object cityAdd(City cityItem) {
    	
    	List<City> cityList = new ArrayList<>();
    	cityList.add(cityItem);
    	
    	cityService.saveCityListInMasterDb(cityList);
    	
    	return ResultVoUtil.success();
    }
	
	@ApiOperation("添加城市信息")
    @OperateLogAnt(logTypeEnum = LogTypeEnum.ADD_LOG, logName = "添加城市信息", logWapperClazz = CityLogWarpperMap.class)
    @PostMapping("/cityAddPostBody")
    public Object cityAddPostBody(@RequestBody City cityItem) {
    	
    	List<City> cityList = new ArrayList<>();
    	cityList.add(cityItem);
    	
    	cityService.saveCityListInMasterDb(cityList);
    	
    	return ResultVoUtil.success();
    }		
    
	// 删
	@ApiOperation("删除城市信息")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = SwaggerParamType.QUERY, name = "cityName", value = "城市名称", required = true, dataType = "String", defaultValue = "苏州"),
	})	
    @OperateLogAnt(logTypeEnum = LogTypeEnum.DELETE_LOG, logName = "删除城市信息", logWapperClazz = CityLogWarpperMap.class)
    @GetMapping("/cityDelete")
    public Object cityDelete(@RequestParam(required = true) String cityName) {
    	
    	cityService.deleteCityInfo(cityName);
    	
    	return ResultVoUtil.success();
    }    
    
    // 查一个
	@ApiOperation("查询城市信息用于修改")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = SwaggerParamType.QUERY, name = "id", value = "记录编号", required = true, dataType = "int", defaultValue = "1"),
	})		
    @OperateLogAnt(logTypeEnum = LogTypeEnum.QUERY_LOG, logName = "查询城市信息用于修改", logWapperClazz = CityLogWarpperMap.class)
    @GetMapping("/citySelectOne")
    public Object citySelectOne(@RequestParam(required = true) Integer id) {

    	List<City> cityList = cityService.getCityInfoWith(id);
    	
    	City cityOld = null;
    	if (ToolUtil.isNotEmpty(cityList)) {
    		cityOld = cityList.get(0);
    	}
    	
    	LogObjectHolder.me().setObject(cityOld);
    	
    	return ResultVoUtil.success(CodeMsgEnum.QUERY_SUCCESS, cityOld);
    }    
    
    // 改
	@ApiOperation("修改城市信息")
	@ApiImplicitParams({
		@ApiImplicitParam(paramType = SwaggerParamType.QUERY, name = "id", value = "记录编号", required = true, dataType = "int", defaultValue = "1"),
		@ApiImplicitParam(paramType = SwaggerParamType.QUERY, name = "provinceId", value = "省份标识", required = true, dataType = "String", defaultValue = "1"), 
		@ApiImplicitParam(paramType = SwaggerParamType.QUERY, name = "cityName", value = "城市名称", required = false, dataType = "String", defaultValue = "北京"),
		@ApiImplicitParam(paramType = SwaggerParamType.QUERY, name = "description", value = "描述", required = false, dataType = "String", defaultValue = "修改测试")	
	})	
    @OperateLogAnt(logTypeEnum = LogTypeEnum.UPDATE_LOG, logName = "修改城市信息", logWapperClazz = CityLogWarpperMap.class)
    @GetMapping("/cityUpdate")
    public Object cityUpdate(City cityNew) {

    	cityService.updateCityInfo(cityNew);
    	
    	return ResultVoUtil.success();
    }    
}
